//
//  UIImage+ImageCompress.m
//  UIIImageCompressExample
//
//  Created by Abraham Kuri on 12/12/13.
//  Copyright (c) 2013 Icalia Labs. All rights reserved.
//

#import "UIImage+ImageCompress.h"

@implementation UIImage (ImageCompress)

+ (UIImage *)bestQualityCompressImage:(UIImage *)image
{
    return [[self class] compressImage:image compressRatio:.85f maxCompressRatio:0.85f];
}

+ (NSData *)bestQualityCompressedImageDataWithImage:(UIImage *)image
{
    return [[self class] compressedImageDataWithImage:image compressRatio:0.85f maxCompressRatio:0.7f];
}

+ (UIImage *)compressImage:(UIImage *)image
             compressRatio:(CGFloat)ratio
{
    return [[self class] compressImage:image compressRatio:ratio maxCompressRatio:.6f];
}

+ (UIImage *)compressImage:(UIImage *)image compressRatio:(CGFloat)ratio maxCompressRatio:(CGFloat)maxRatio
{
    NSData *data = [[self class] compressedImageDataWithImage:image compressRatio:ratio maxCompressRatio:maxRatio];
    if (data) {
        return [UIImage imageWithData:data];
    }
    return nil;
}

+ (NSData *)compressedImageDataWithImage:(UIImage *)image compressRatio:(CGFloat)ratio maxCompressRatio:(CGFloat)maxRatio
{
    int MAX_UPLOAD_SIZE = 1024 * 1024;
    
    // Determine resolution
    CGFloat maxWidth = 750.f;
    CGFloat maxHeight = 1080.f;
    
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;

    CGFloat widthRatio = width / maxWidth;
    CGFloat heightRatio = height / maxHeight;
    CGFloat maxResolutionRatio = MAX(widthRatio, heightRatio);
    // If any side exceeds the maximun size, reduce the greater side to 750px width or 1080px height
    if (maxResolutionRatio > 1.f) {
        image = [self scaleDown:image withSize:CGSizeMake(width / maxResolutionRatio,  height / maxResolutionRatio)];
    }
    //Compression settings
    CGFloat compression = ratio;
    CGFloat maxCompression = maxRatio;
    
    NSData *imageData = nil;
    if (maxResolutionRatio > 1.f) {
        imageData = UIImageJPEGRepresentation(image, compression);
    } else {
        imageData = UIImageJPEGRepresentation(image, 1);
    }
    
    while ([imageData length] > MAX_UPLOAD_SIZE && compression > maxCompression) {
        compression -= 0.10;
        imageData = UIImageJPEGRepresentation(image, compression);
    }
    
    //Retuns the compressed image
    return imageData;
}

+ (UIImage *)scaleDown:(UIImage *)image withSize:(CGSize)newSize
{
    //We prepare a bitmap with the new size
    UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);

    //Draws a rect for the image
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

    //We set the scaled image from the context
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return scaledImage;
}

-(UIImage *)compresstoTargetWidth:(CGFloat)targetWidth {
    CGSize imageSize = self.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [self drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
